<!-- 劳务公司管理 -->
<template>
    <div >
        <div class='table-page-search-wrapper'>
            <a-form layout='inline'>
               <a-row :gutter='48'>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="公司名称">
                            <a-input v-model="companyName" placeholder="请输入公司名称" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="统一社会信用代码">
                            <a-input v-model="socialCode" placeholder="请输入统一社会信用代码" allow-clear></a-input>
                        </a-form-item>
                    </a-col><a-col :md='8' :sm='24'>
                        <a-form-item label="手机号">
                            <a-input v-model="contactPhone" placeholder="请输入手机号" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="姓名">
                            <a-input v-model="userName" placeholder="请输入姓名" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="状态">
                            <a-select v-model="state" placeholder="请选择" @change="getData" allow-clear>
                                <a-select-option v-for="item in statusList" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='16' :sm='24'>
                        <a-form-item label="创建时间">
                            <a-range-picker :show-time="{ format: 'HH:mm:ss' }" format="YYYY-MM-DD HH:mm:ss" @ok="onDateChange" allow-clear/>
                        </a-form-item>
                    </a-col>
                    <!-- <a-col :md='16' :sm='24'>
                    </a-col> -->
                    <a-col :md='8' :sm='24'>
                        <a-form-item >
                            <a-button type='primary' @click="getData">查询</a-button>
                            <a-button style="margin-left: 20px" type='' @click="add">创建税源地公司</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>  

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ 'total': total ,'showSizeChanger': true,'pageSize': size,}" size="small" :scroll="{ x: 1300, }">
            <div slot="createdBy" slot-scope="createdBy,record">
                <div>{{record.updatedBy ?? createdBy}}</div>
            </div>
            <div slot="industrialParkName" slot-scope="industrialParkName">
                <a-tag :color="colors[0]">{{industrialParkName}}</a-tag>
            </div>
            <div slot="state" slot-scope="state">
                <a-badge :status="state | statusTypeFilter" :text="state | statusFilter" />
            </div>
            <div slot="operation" slot-scope="text,record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="detail(record)">编辑</a-button>
                    <a-button type="link" @click="related(record)">关联园区</a-button>
                    <a-button v-if="record.state == 0" type="link" @click="updateState(record,1)">启用</a-button>
                    <a-button v-else type="link" @click="updateState(record,0)">停用</a-button>
                </div>
            </div>
        </a-table>

        <a-modal 
            v-model="visible" 
            :title="isAdd ? '添加税源地' : '编辑税源地'"
            @ok="handleOk" 
            :forceRender="true"
            @cancel="visible = false">
            <div>
                <a-form-model :model="info" ref="ruleForm" :rules="rules" :label-col="{ span: 8 }" :wrapper-col="{ span: 15 }">
                    <a-form-model-item label="客户名称" prop="companyName">
                        <a-input v-model="info.companyName" :maxLength="40" :disabled="!isAdd" allow-clear placeholder="请输入客户名称" />
                    </a-form-model-item>
                    <a-form-model-item label="统一社会信用代码" prop="socialCode">
                        <a-input v-model="info.socialCode" :disabled="!isAdd" allow-clear placeholder="请输入统一社会信用代码" />
                    </a-form-model-item>
                    <a-form-model-item label="姓名" prop="userName">
                        <a-input v-model="info.userName" allow-clear placeholder="请输入姓名" />
                    </a-form-model-item>
                    <a-form-model-item label="手机号" prop="contactPhone">
                        <a-input v-model="info.contactPhone" allow-clear placeholder="请输入手机号" />
                    </a-form-model-item>
                    <a-form-model-item v-if="isAdd" prop="password" label="初始密码" help="未填写默认88888888，6-20位，支持数字，字母，区分大小写">
                        <a-input v-model="info.password" allow-clear placeholder="请输入初始密码" />
                    </a-form-model-item>
                </a-form-model>
            </div>
        </a-modal>

        <!-- 关联园区 -->
        <a-modal 
            v-model="relatedVisible" 
            :title="'关联园区'"
            @ok="relatedHandleOk" 
            @cancel="relatedVisible = false">
            <div>
                <a-form-model :model="info" ref="ruleForm" :rules="rules" :label-col="{ span: 8 }" :wrapper-col="{ span: 15 }">
                    <a-form-model-item label="园区信息" prop="companyName">
                        <a-select show-search  v-model="industrialParkId" placeholder="请选择园区">
                            <a-select-option v-for="(itm) in selIndustrialList" :value="itm.id" :key="itm.id">{{itm.parkName}}</a-select-option>
                        </a-select>   
                    </a-form-model-item>
                </a-form-model>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { queryServicesList , updateServicesState , createServices , updateServices , companyRelatedIndustrial } from '@/api/customer';
import { selIndustrialList } from '@/api/industrial';
export default {
    data () {
        return {
            companyName: '', // 公司名称
            socialCode: '', // 统一社会信用代码
            userName: '', // 姓名
            contactPhone: '', // 联系手机号
            columns,
            page: 1,
            size: 10,
            total: 0,
            isload: false,
            dataList: [],
            state: undefined,
            statusList: [
                { id: 0, name: '已停用'},
                { id: 1, name: '使用中'},
            ],
            visible: false,
            isAdd: true,
            form: this.$form.createForm(this),
            info: {
                id: '',
                companyName: '',
                socialCode: '',
                userName: '',
                contactPhone: '',
                password: '',
            },
            startTime: '',
            endTime: '',
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            rules: {
                companyName: [
                    { required: true, message: '请输入客户名称', trigger: 'blur' },
                    { max: 40, message: '客户名称最长不能超过40', trigger: 'blur' },
                ],
                socialCode: [{ required: true, message: '请输入统一社会信用代码', trigger: 'blur' }],
                userName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
                contactPhone: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    { pattern: /^1[3456789]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'}
                ],
            },
            relatedVisible: false,
            industrialParkId: '', // 关联园区ID
            selIndustrialList: [],
        };
    },
    created(){
        this.getData();
    },
    methods: {
        // 日期筛选
        onDateChange(date,dateStr){
            console.log(date,dateStr)
            if(date.length > 0){
                // this.startTime = `${dateStr[0]} 00:00:00`
                // this.endTime = `${dateStr[1]} 23:59:59`
                this.startTime = date[0].format('YYYY-MM-DD HH:mm:ss')
                this.endTime = date[1].format('YYYY-MM-DD HH:mm:ss')
            }else{
                this.startTime = ''
                this.endTime = ''
            }
            this.getData()
        },
        // 获取账户列表数据
        getData(){
            let _this = this
            _this.isload = true;
            const params = {
                'startTime': _this.startTime,
                'endTime': _this.endTime,
                'companyName': _this.companyName, // 公司名称
                'socialCode': _this.socialCode,
                'userName': _this.userName,
                'contactPhone': _this.contactPhone,
                'state': _this.state,
                'page': _this.page - 1,
                'size': _this.size,
            }
            queryServicesList(params)
            .then(res=>{
                _this.isload = false;
                if(res.success){
                    _this.dataList = res.data;
                    _this.total = res.totalCount;
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加
        add(){
            this.isAdd = true;
            this.$refs.ruleForm.resetFields();
            this.info.id = ''
            // this.info.companyName = ''
            // this.info.socialCode = ''
            // this.info.userName = ''
            // this.info.contactPhone = ''
            // this.info.password = ''
            this.visible = true;
        },
        // 修改
        detail(item){
            this.info = JSON.parse(JSON.stringify(item))
            this.isAdd = false;
            this.visible = true;
        },
        // 确认添加、修改
        handleOk(){
            let _this = this
            console.log(_this.info,'info')
            _this.$refs.ruleForm.validate(valid => {
                console.log(valid)
                if (valid) {
                    var apiUrl = _this.isAdd ? createServices(_this.info) : updateServices(_this.info)
                    apiUrl.then(res=>{
                        if(res.success){
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: _this.isAdd == 1 ? '添加成功' : '修改成功',
                                duration: 8
                            })
                            _this.visible = false;
                            _this.getData()
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                }
            });
        },
        // 状态切换
        updateState(item,state){
            let _this = this
            _this.$confirm({
                title: '温馨提示',
                content: state == 1 ? '是否确认开启当前劳务公司' : '是否确认关闭当前劳务公司',
                okText: state == 1 ? '确认开启' : '确认关闭',
                onOk() {
                    const params = {
                        'id': item.id,
                        'state': state,
                    }
                    updateServicesState(params)
                    .then(res=>{
                        if(res.success){
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: state == 1 ? '开启成功' : '关闭成功',
                                duration: 8
                            })
                            _this.getData()
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                },
                onCancel() {},
            });
        },
        // 关联园区
        related(item){
            this.info = JSON.parse(JSON.stringify(item));
            this.industrialParkId = item.industrialParkId;
            this.getSelIndustrialList();
            this.relatedVisible = true;
        },
        // 确认关联园区
        relatedHandleOk(){
            let _this = this;
            if(!this.industrialParkId || this.industrialParkId == ''){
                this.$message.warning('请选择关联的园区');
                return
            }
            const params = {
                'laborCompanyId': _this.info.id,
                'industrialParkId': _this.industrialParkId,
            }
            companyRelatedIndustrial(params)
            .then(res => {
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: '关联成功',
                        duration: 8,
                    })
                    _this.relatedVisible = false
                    _this.getData()
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 获取园区信息
        getSelIndustrialList(){
            let _this = this;
            selIndustrialList({})
            .then(res => {
                if(res.success){
                    _this.selIndustrialList = res.data;
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        }
    }
}

</script>
<style scoped>

</style>